<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class close_reminder extends Page
{
       public $hiddenReminder = null;
       public $hiddenVeh = null;
       function close_reminderCreate($sender, $params)
       {
       $veh = $_GET['veh'];
       $reminder = $_GET['reminder'];
       $this->hiddenVeh->setValue($veh);
       $this->hiddenReminder->setValue($reminder);

       $result_close = mysql_query("update veh_reminder_tbl set
       status2 = 'Complete' where reminder_id='".$this->hiddenReminder->getValue()."'");


       // create new veh_reminder
       $query_rec_reminder = "select * from veh_reminder_tbl where reminder_id='".$this->hiddenReminder->getValue()."'";
       //die($query_rec_reminder);
       $result_rec_query = mysql_query($query_rec_reminder)or die ("veh_remind - ".mysql_error());
       $row_rec_reminder = mysql_fetch_array($result_rec_query);

       $temp_rec_id = $row_rec_reminder['recurring_id'];

       $query_rec = "select * from template_recurring_tbl where id='".$temp_rec_id."'";
       //die($query_rec_reminder);
       $result_rec = mysql_query($query_rec)or die ("veh_remind - ".mysql_error());
       $row_rec = mysql_fetch_array($result_rec);

       $query_veh = "select * from vehicle_tbl where vehicle_id='".$this->hiddenVeh->getValue()."'";
       //die($query_rec_reminder);
       $result_veh = mysql_query($query_veh)or die ("vehicle - ".mysql_error());
       $row_veh = mysql_fetch_array($result_veh);


       $flag = $row_rec_reminder['flag_temp'];

       if($flag=="1")
       {

       $sched_by = $row_rec['schedule_by'];

       $temp_od = $row_rec['due_usage'];
       $veh_od = $row_rec_reminder['due_hours'];
       $new_od = $veh_od+$temp_od;


       if($sched_by=="Mileage" || $sched_by=="Hours")
       {
       $odometer = $new_od;
       }

       if($sched_by=="Time")
       {
       $odometer = '0';
       }


       $basis = date('Y-m-d');
       $days_mos = $row_rec['due_by'];
       $val = $row_rec['due_time'];

       if($days_mos=='Days')
       {
       $date = date($basis);
       $date_computed = strtotime(date("Y-m-d", strtotime($date)) . "+".$val." day");
       $due_date = date("Y-m-d", $date_computed);
       }

       if($days_mos=='Months')
       {
       $date = date($basis);
       $date_computed = strtotime(date("Y-m-d", strtotime($date)) . "+".$val." month");
       $due_date = date("Y-m-d", $date_computed);
       }


       if($sched_by=='Mileage' || $sched_by=='Hours')
       {
         $save_rem = "rem_hours,";
         $save_rem2 = "'".$row_rec['rem_hours']."',";
       }
       if($sched_by=='Time')
       {
         $save_rem = "rem_by,rem_time,";
         $save_rem2 = "'".$row_rec['rem_by']."','".$row_rec['rem_time']."',";
       }

       $result_rec = mysql_query("insert into veh_reminder_tbl (company_id,
       vehicle_id,
       schedule_by,
       due_hours,
       due_date,
       ".$save_rem."
       completion_window,
       type,
       description,
       price,
       status2,
       flag_temp,
       recurring_id)

       values ('".$_SESSION['company_id']."',
       '".$this->hiddenVeh->getValue()."',
       '".$row_rec_reminder['schedule_by']."',
       '".$odometer."',
       '".$due_date."',
       ".$save_rem2."
       '".$row_rec_reminder['completion_window']."',
       '".$row_rec_reminder['type']."',
       '".$row_rec_reminder['description']."',
       '".$row_rec_reminder['price']."',
       'Pending',
       '1',
       '".$row_rec['id']."')")or die("insert reminder - ".mysql_error());
       //die($new_od);
       //die($query_ins_remin);
       //$result = mysql_query($query_ins_remin)or die("INSERT REMINDER - " . mysql_error());
       }

         if($row_rec_reminder['recur_per_veh']!='0')
         {
         $sched_by = $row_rec_reminder['schedule_by'];

         $every_od = $row_rec_reminder['every_hours'];
         $due_od = $row_rec_reminder['due_hours'];
         $new_od = $due_od+$every_od;


         if($sched_by=="Mileage" || $sched_by=="Hours")
         {
         $odometer_veh = $new_od;
         }

         if($sched_by=="Time" || $sched_by=="Date")
         {
         $odometer_veh = '0';
         }


         $basis_date = date('Y-m-d');
         $days_mos = $row_rec_reminder['every_by'];
         $val = $row_rec_reminder['every_date'];

         if($days_mos=='Days')
         {
         $date = date($basis_date);
         $date_computed = strtotime(date("Y-m-d", strtotime($date)) . "+".$val." day");
         $due_date_veh = date("Y-m-d", $date_computed);
         }

         if($days_mos=='Months')
         {
         $date = date($basis_date);
         $date_computed = strtotime(date("Y-m-d", strtotime($date)) . "+".$val." month");
         $due_date_veh = date("Y-m-d", $date_computed);
         }


         if($sched_by=='Mileage' || $sched_by=='Hours')
         {
           $save_rem = "rem_hours,";
           $save_rem2 = "'".$row_rec_reminder['rem_usage']."',";
         }
         if($sched_by=='Time' || $sched_by=='Date')
         {
           $save_rem = "rem_by,rem_time,";
           $save_rem2 = "'".$row_rec_reminder['rem_by']."','".$row_rec_reminder['rem_time']."',";
         }

         mysql_query("insert into veh_reminder_tbl (company_id,
         vehicle_id,
         schedule_by,
         recur_per_veh,
         due_hours,
         due_date,
         every_hours,
         every_date,
         every_by,
         ".$save_rem."
         completion_window,
         type,
         description,
         vendor,
         comments,
         status2
         )

         values ('".$_SESSION['company_id']."',
         '".$this->hiddenVeh->getValue()."',
         '".$sched_by."',
         '1',
         '".$odometer_veh."',
         '".$due_date_veh."',
         '".$row_rec_reminder['every_hours']."',
         '".$row_rec_reminder['every_date']."',
         '".$row_rec_reminder['every_by']."',
         ".$save_rem2."
         '".$row_rec_reminder['completion_window']."',
         '".$row_rec_reminder['type']."',
         '".$row_rec_reminder['description']."',
         '".$row_rec_reminder['vendor']."',
         '".$row_rec_reminder['comments']."',
         'Pending')")or die("insert reminder - ".mysql_error());
         }

       if($flag!="1")
       {
       }

       // end veh_reminder


       echo '<script language=javascript>
       alert("Reminder completed!");
       //window.opener.location.reload();
       //window.top.location.reload();
       window.close()
       window.top.opener.top.location.reload();
       </script>';


       }

}

global $application;

global $close_reminder;

//Creates the form
$close_reminder=new close_reminder($application);

//Read from resource file
$close_reminder->loadResource(__FILE__);

//Shows the form
$close_reminder->show();

?>